<style lang="less">
    @import './login.less';
</style>

<template>

    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con" style="width: 500px;top: 70%;">
            <Card>
                <!-- <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p> -->
                <Tabs value="examer"  >
                  <TabPane style="font-size:16px;" label="考生登录" name="examer">
                    <div style="font-size:16px;" class="form-con">
                        <Form :model="examinee">
                            <FormItem>
                                <Input v-model="examinee.usernum" placeholder="警号">
                                    <span slot="prepend">
                                        <Icon :size="16" type="ribbon-b"></Icon>
                                    </span>
                                </Input>
                            </FormItem>
                            <FormItem>
                                <Input type="text" v-model="examinee.idnumber" placeholder="身份证">
                                    <span slot="prepend">
                                        <Icon :size="14" type="person"></Icon>
                                    </span>
                                </Input>
                            </FormItem>
                            <FormItem prop="cannumber">
                                <Input type="text" v-model="examinee.password" placeholder="密码">
                                    <span slot="prepend">
                                        <Icon :size="14" type="locked"></Icon>
                                    </span>
                                </Input>
                            </FormItem>
                            <FormItem>
                                <Button style="font-size:16px;" @click="handleExam" type="primary" long>登录考试系统</Button>
                            </FormItem>
                        </Form>
                        <!-- <p class="login-tip">输入任意用户名和密码即可</p> -->
                    </div>
                  </TabPane>
                      <TabPane  label="管理员登录" name="admin">
                        <div class="form-con">
                            <Form ref="loginForm" :model="form" :rules="rules">
                                <FormItem prop="userName">
                                    <Input  style="font-size:16px;" v-model="form.username" placeholder="请输入用户名">
                                        <span slot="prepend">
                                            <Icon :size="16" type="person"></Icon>
                                        </span>
                                    </Input>
                                </FormItem>
                                <FormItem prop="password">
                                    <Input style="font-size:16px;" type="password" v-model="form.password" placeholder="请输入密码">
                                        <span slot="prepend">
                                            <Icon :size="14" type="locked"></Icon>
                                        </span>
                                    </Input>
                                </FormItem>
                                <FormItem>
                                    <Button @click="handleSubmit" type="primary" long style="font-size:16px;">登录管理系统</Button>
                                </FormItem>
                            </Form>
                            <!-- <p class="login-tip">输入任意用户名和密码即可</p> -->
                        </div>

                      </TabPane>

                  </Tabs>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie';
import axios from 'axios'
export default {
    data () {
        return {
            form: {
                username: '',
                password: ''
            },
            examinee: {
              usernum: '175326',
              idnumber: '',
              password: '0',
            },
            rules: {
                username: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
                // cannumber: [
                //     { required: true, message: '考号不能为空', trigger: 'blur' }
                // ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                  axios.post('rs/logic/login', {
                    username: this.form.username,
                    password: this.form.password,
                    usertype: '管理员'
                  }).then(res => {
                    if (res.data.code === 200) {
                      if (res.data.code.userstatus !== '1') {
                        Cookies.set('user', this.form.username);
                        Cookies.set('userobj', res.data);
                        Cookies.set('password', this.form.password);
                        Cookies.set('access', 0); // 没有权限管理
                        this.$store.commit('userLogin', res.data)
                        this.$router.push({
                            name: 'home_index'
                          });
                        }
                        else {
                          this.$Message.error('该账号已经登陆，请确认用户名和密码');
                        }
                    } else {
                      this.$Message.error('登录失败，请仔细确认用户名和密码');
                    }
                  }).catch((error) => {
                    // console.log('11111111', error);
                    this.$Message.error('登录失败，请仔细确认用户名和密码');
                  })

                  // Cookies.set('user', this.form.username);
                  // Cookies.set('password', this.form.password);
                  // Cookies.set('access', 0); // 没有权限管理
                  // this.$router.push({
                  //     name: 'home_index'
                  // });
                }
            });
        },
        handleExam () {
          if (this.examinee.usernum.length === 0 && this.examinee.idnumber.length === 0) {
            this.$Message.error('请填写警号或身份证号');
          }
          axios.post('rs/logic/login', {
            usernum: this.examinee.usernum,
            idnumber: this.examinee.idnumber,
            password: this.examinee.password,
            usertype: '考生'
          }).then((res) => {
            if (res.data.code === 200) {
              if (res.data.code.userstatus !== '1') {
                Cookies.remove('user');
                Cookies.remove('password');
                Cookies.set('examinee', this.examinee)
                Cookies.set('examineeinfo', res.data)
                this.$store.commit('examLogin', res.data)
                this.$router.push({
                    name: 'examperson',
                    params: res.data
                });
              } else {
                this.$Message.error('该账号已经登陆，请确认用户名和密码');
              }


            } else {
                this.$Message.error('登陆失败，请注意查看输入的用户名或密码');
            }
          }).catch((error) => {
              this.$Message.error('登陆失败');
          })


          // Cookies.set('examinee', {a: 1})
          // Cookies.set('examineeinfo', {a: 1})
          // this.$router.push({
          //     name: 'examperson'
          // });
        },
        handleFullscreen () {
          var el = document.body

          if (el.requestFullscreen) {
              el.requestFullscreen()
            } else if (el.mozRequestFullScreen) {
              el.mozRequestFullScreen()
            } else if (el.webkitRequestFullScreen) {
              el.webkitRequestFullScreen()
            } else if (el.msRequestFullscreen) {
              el.msRequestFullscreen()
            }
        },
    }
};
</script>

<style>

</style>
